package chances.epg.user.service.rdb;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import chances.epg.data.PageBean;
import chances.epg.entity.user.UserCollection;
import chances.epg.user.service.CollectionService;

@Service("collectionService")
public class RdbCollectionService extends BaseDao implements CollectionService {

	@Override
	public void addCollection(UserCollection collection) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("obj", collection);
		try{
		    this.insert("insertUserCollection", paramsMap);
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserCollection> listCollections(String userId, String contentType,Date beginTime,
			Date endTime, PageBean page) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("userId", userId);
		
		if(beginTime != null) {
			paramsMap.put("beginTime", beginTime);
		}
		
		if(endTime != null) {
			paramsMap.put("endTime", endTime);
		}
		
		if(StringUtils.isNotBlank(contentType)){
			paramsMap.put("contentType", contentType);
		}
		
		try{
			if(page != null){
			    return (List<UserCollection>)this.queryForList("queryUserCollection", paramsMap,0,page);
			}else{
				return (List<UserCollection>)this.queryForList("queryUserCollection", paramsMap);
			}
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<UserCollection> listCollections(String userId, String contentType,String category,Date beginTime,
			Date endTime, PageBean page) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("userId", userId);
		
		if(beginTime != null) {
			paramsMap.put("beginTime", beginTime);
		}
		
		if(endTime != null) {
			paramsMap.put("endTime", endTime);
		}
		
		if(StringUtils.isNotBlank(contentType)){
			paramsMap.put("contentType", contentType);
		}
		
		if(StringUtils.isNotBlank(category)){
			paramsMap.put("category", category);
		}
		
		try{
			if(page != null){
			    return (List<UserCollection>)this.queryForList("queryUserCollection", paramsMap,0,page);
			}else{
				return (List<UserCollection>)this.queryForList("queryUserCollection", paramsMap);
			}
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}

	@Override
	public void removeCollection(String userId, String contentId) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("userId", userId);
		paramsMap.put("contentId", contentId);
		try{
		    this.delete("deleteUserCollection", paramsMap);
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	@Override
	public void batchDelUserCollection(String[] codes, String userId) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("codes", codes);
		paramsMap.put("userId", userId);
		try{
		    this.delete("batchDelUserCollection", paramsMap);
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	

	@Override
	public void clearCollections(String userId,String category) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("userId", userId);
		
		if(StringUtils.isNotBlank(category)){
			paramsMap.put("category", category);
		}
		
		try{
		    this.delete("clearUserCollection", paramsMap);
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserCollection> listCollections(String userId, String contentId) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("userId", userId);
		paramsMap.put("contentId", contentId);
		
		try{
			List<UserCollection> result = (List<UserCollection>)this.queryForList("queryCollectionsByUserIdAndContentId", paramsMap);
			return result;
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}


}
